home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint99s / filesys.c < prev    next >
C/C++ Source or Header  |  1993-01-16  |  27KB  |  1,100 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992 Atari Corp.
  4. All rights reserved.
  5. */
  6.  
  7. /*
  8.  * various file system interface things
  9.  */
  10.  
  11. #include "mint.h"
  12.  
  13. #define PATH2COOKIE_DB(x) TRACE(x)
  14.  
  15. FILESYS *active_fs;
  16. FILESYS *drives[NUM_DRIVES];
  17. extern FILESYS tos_filesys;    /* declaration needed for debugging only */
  18.  
  19. /* "aliased" drives are different names
  20.  * for real drives/directories
  21.  * if drive d is an alias for c:\usr,
  22.  * then alias_drv[3] == 2 (the real
  23.  * drive) and aliases has bit (1L << 3)
  24.  * set.
  25.  * NOTE: if aliasdrv[d] is 0, then d is not an aliased drive,
  26.  * otherwise d is aliased to drive aliasdrv[d]-1
  27.  * (e.g. if drive A: is aliased to B:\FOO, then
  28.  * aliasdrv[0] == 'B'-'A'+1 == 2). Always remember to
  29.  * compensate for the extra 1 when dereferencing aliasdrv!
  30.  */
  31. int    aliasdrv[NUM_DRIVES];
  32.  
  33. FILEPTR *flist;        /* a list of free file pointers */
  34.  
  35. char follow_links[1];    /* dummy "name" used as a parameter to path2cookie */
  36.  
  37. /* vector of valid drives, according to GEMDOS */
  38. /* note that this isn't necessarily the same as what the BIOS thinks of
  39.  * as valid
  40.  */
  41. long dosdrvs;
  42.  
  43. /*
  44.  * Initialize a specific drive. This is called whenever a new drive
  45.  * is accessed, or when media change occurs on an old drive.
  46.  * Assumption: at this point, active_fs is a valid pointer
  47.  * to a list of file systems.
  48.  */
  49.  
  50. /* table of processes holding locks on drives */
  51. extern PROC *dlockproc[];    /* in dosdir.c */
  52.  
  53. void
  54. init_drive(i)
  55.     int i;
  56. {
  57.     long r;
  58.     FILESYS *fs;
  59.     fcookie root_dir;
  60.  
  61.     TRACE(("init_drive(%c)", i+'A'));
  62.  
  63.     drives[i] = 0;        /* no file system */
  64.     if (i >= 0 && i < NUM_DRIVES) {
  65.         if (dlockproc[i]) return;
  66.     }
  67.  
  68.     for (fs = active_fs; fs; fs = fs->next) {
  69.         r = (*fs->root)(i, &root_dir);
  70.         if (r == 0) {
  71.             drives[i] = root_dir.fs;
  72.             release_cookie(&root_dir);
  73.             break;
  74.         }
  75.     }
  76. }
  77.  
  78. /*
  79.  * initialize the file system
  80.  */
  81.  
  82. #define NUMFPS    40    /* initial number of file pointers */
  83.  
  84. void
  85. init_filesys()
  86. {
  87.     static FILEPTR initial[NUMFPS+1];
  88.     int i;
  89.     extern FILESYS tos_filesys, bios_filesys, pipe_filesys,
  90.         proc_filesys, uni_filesys;
  91.  
  92. /* get the vector of connected GEMDOS drives */
  93.     dosdrvs = Dsetdrv(Dgetdrv()) | drvmap();
  94.  
  95. /* set up some initial file pointers */
  96.     for (i = 0; i < NUMFPS; i++) {
  97.         initial[i].devinfo = (ulong) (&initial[i+1]);
  98.     }
  99.     initial[NUMFPS].devinfo = 0;
  100.     flist = initial;
  101.  
  102. /* set up the file systems */
  103.     tos_filesys.next = 0;
  104.     bios_filesys.next = &tos_filesys;
  105.     pipe_filesys.next = &bios_filesys;
  106.     proc_filesys.next = &pipe_filesys;
  107.     uni_filesys.next = &proc_filesys;
  108.  
  109.     active_fs = &uni_filesys;
  110.  
  111. /* initialize the BIOS file system */
  112.     biosfs_init();
  113.  
  114. /* initialize the unified file system */
  115.     unifs_init();
  116. }
  117.  
  118. /*
  119.  * load file systems from disk
  120.  * this routine is called after process 0 is set up, but before any user
  121.  * processes are run
  122.  *
  123.  * NOTE that a number of directory changes take place here: we look first
  124.  * in the current directory, then in the directory \mint, and finally
  125.  * the d_lock() calls force us into the root directory.
  126.  */
  127.  
  128. typedef FILESYS * ARGS_ON_STACK (*FSFUNC) P_((struct kerinfo *));
  129.  
  130. void
  131. load_filesys()
  132. {
  133.     long r;
  134.     BASEPAGE *b;
  135.     FILESYS *fs;
  136.     FSFUNC initf;
  137.     static DTABUF dta;
  138.     int i;
  139.     extern struct kerinfo kernelinfo; /* in main.c */
  140.     char curpath[PATH_MAX];
  141.     MEMREGION *xfsreg;
  142. #define NPATHS 3
  143.     static const char *paths[NPATHS] = {"", "\\MINT", "\\MULTITOS"};
  144.  
  145.     curproc->dta = &dta;
  146.     d_getpath(curpath,0);
  147.  
  148.     for (i = 0; i < NPATHS; i++) {
  149.         if (*paths[i]) {
  150. /* don't bother checking the current directory twice! */
  151.             if (!stricmp(paths[i],curpath))
  152.             r = -1;
  153.             else
  154.             r = d_setpath(paths[i]);
  155.         }
  156.         else
  157.             r = 0;
  158.  
  159.         if (r == 0)
  160.             r = f_sfirst("*.xfs", 0);
  161.  
  162.         while (r == 0) {
  163.         b = (BASEPAGE *)p_exec(3, dta.dta_name, (char *)"", (char *)0);
  164.         if ( ((long)b) < 0 ) {
  165.             DEBUG(("Error loading file system %s", dta.dta_name));
  166.             r = f_snext();
  167.             continue;
  168.         }
  169.     /* we leave a little bit of slop at the end of the loaded stuff */
  170.         m_shrink(0, (virtaddr)b, 512 + b->p_tlen + b->p_dlen + b->p_blen);
  171.         initf = (FSFUNC)b->p_tbase;
  172.         TRACE(("initializing %s", dta.dta_name));
  173.         fs = (*initf)(&kernelinfo);
  174.  
  175.         if (fs) {
  176.             TRACE(("%s loaded OK", dta.dta_name));
  177.     /* put the loaded XFS into super accesible memory */
  178.             xfsreg = addr2region( (long) b );
  179.             mark_region(xfsreg, PROT_S);
  180.     /* link it into the list of drivers */
  181.             fs->next = active_fs;
  182.             active_fs = fs;
  183.         } else {
  184.             DEBUG(("%s returned null", dta.dta_name));
  185.             m_free((virtaddr)b);
  186.         }
  187.         r = f_snext();
  188.         }
  189.     }
  190.  
  191. /* here, we invalidate all old drives EXCEPT for ones we're already using (at
  192.  * this point, only the bios devices should be open)
  193.  * this gives newly loaded file systems a chance to replace the
  194.  * default tosfs.c
  195.  */
  196.     for (i = 0; i < NUM_DRIVES; i++) {
  197.         if (d_lock(1, i) == 0)    /* lock if possible */
  198.             d_lock(0, i);    /* and then unlock */
  199.     }
  200. }
  201.  
  202. void
  203. close_filesys()
  204. {
  205.     PROC *p;
  206.     FILEPTR *f;
  207.     int i;
  208.  
  209.     TRACE(("close_filesys"));
  210. /* close every open file */
  211.     for (p = proclist; p; p = p->gl_next) {
  212.         for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
  213.             if ( (f = p->handle[i]) != 0) {
  214.                 if (p->wait_q == TSR_Q || p->wait_q == ZOMBIE_Q)
  215.                     ALERT("Open file for dead process?");
  216.                 do_pclose(p, f);
  217.             }
  218.         }
  219.     }
  220. }
  221.  
  222. /*
  223.  * "media change" routine: called when a media change is detected on device
  224.  * d, which may or may not be a BIOS device. All handles associated with
  225.  * the device are closed, and all directories invalidated. This routine
  226.  * does all the dirty work, and is called automatically when
  227.  * disk_changed detects a media change.
  228.  */
  229.  
  230. void ARGS_ON_STACK 
  231. changedrv(d)
  232.     unsigned d;
  233. {
  234.     PROC *p;
  235.     int i;
  236.     FILEPTR *f;
  237.     FILESYS *fs;
  238.     DIR *dirh;
  239.     fcookie dir;
  240.     int warned = (d & 0xf000) == PROC_BASE_DEV;
  241.     long r;
  242.  
  243. /* if an aliased drive, change the *real* device */
  244.     if (d < NUM_DRIVES && aliasdrv[d]) {
  245.         d = aliasdrv[d] - 1;    /* see NOTE above */
  246.     }
  247.  
  248. /* re-initialize the device, if it was a BIOS device */
  249.     if (d < NUM_DRIVES) {
  250.         fs = drives[d];
  251.         if (fs) {
  252.             (void)(*fs->dskchng)(d);
  253.         }
  254.         init_drive(d);
  255.     }
  256.  
  257.     for (p = proclist; p; p = p->gl_next) {
  258.     /* invalidate all open files on this device */
  259.         for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
  260.             if (((f = p->handle[i]) != 0) && (f->fc.dev == d)) {
  261.                 if (!warned) {
  262.                 ALERT(
  263. "Files were open on a changed drive (0x%x)!", d);
  264.                 warned++;
  265.                 }
  266.  
  267. /* we set f->dev to NULL to indicate to do_pclose that this is an
  268.  * emergency close, and that it shouldn't try to make any
  269.  * calls to the device driver since the file has gone away
  270.  */
  271.                 f->dev = NULL;
  272.                 (void)do_pclose(p, f);
  273. /* we could just zero the handle, but this could lead to confusion if
  274.  * a process doesn't realize that there's been a media change, Fopens
  275.  * a new file, and gets the same handle back. So, we force the
  276.  * handle to point to /dev/null.
  277.  */
  278.                 p->handle[i] =
  279.                 do_open("U:\\DEV\\NULL", O_RDWR, 0, (XATTR *)0);
  280.             }
  281.         }
  282.  
  283.     /* terminate any active directory searches on the drive */
  284.     /* BUG: This handles only Fsfirst/Fsnext searches! */
  285.         for (i = 0; i < NUM_SEARCH; i++) {
  286.             dirh = &p->srchdir[i];
  287.             if (dirh->fc.fs && dirh->fc.dev == d) {
  288.                 TRACE(("closing search for process %d", p->pid));
  289.                 release_cookie(&dirh->fc);
  290.                 dirh->fc.fs = 0;
  291.                 p->srchdta[i] = 0;
  292.             }
  293.         }
  294.  
  295.         if (d >= NUM_DRIVES) continue;
  296.  
  297.     /* change any active directories on the device to the (new) root */
  298.         fs = drives[d];
  299.         if (fs) {
  300.             r = (*fs->root)(d, &dir);
  301.             if (r != E_OK) dir.fs = 0;
  302.         } else {
  303.             dir.fs = 0; dir.dev = d;
  304.         }
  305.  
  306.         for (i = 0; i < NUM_DRIVES; i++) {
  307.             if (p->root[i].dev == d)
  308.                 dup_cookie(&p->root[i], &dir);
  309.             if (p->curdir[i].dev == d)
  310.                 dup_cookie(&p->curdir[i], &dir);
  311.         }
  312.         release_cookie(&dir);
  313.     }
  314. }
  315.  
  316. /*
  317.  * check for media change: if the drive has changed, call changedrv to
  318.  * invalidate any open files and file handles associated with it, and
  319.  * call the file system's media change routine.
  320.  * returns: 0 if no change, 1 if change
  321.  */
  322.  
  323. int
  324. disk_changed(d)
  325.     int d;
  326. {
  327.     short r;
  328.     FILESYS *fs;
  329.     static char tmpbuf[8192];
  330.  
  331. /* for now, only check BIOS devices */
  332.     if (d < 0 || d >= NUM_DRIVES)
  333.         return 0;
  334. /* watch out for aliased drives */
  335.     if (aliasdrv[d]) {
  336.         d = aliasdrv[d] - 1;
  337.         if (d < 0 || d >= NUM_DRIVES)
  338.             return 0;
  339.     }
  340.  
  341. /* has the drive been initialized yet? If not, then initialize it and return
  342.  * "no change"
  343.  */
  344.     fs = drives[d];
  345.     if (!fs) {
  346.         TRACE(("drive %c not yet initialized", d+'A'));
  347.         changedrv(d);
  348.         return 0;
  349.     }
  350.  
  351. /* We have to do this stuff no matter what, because someone may have installed
  352.  * vectors to force a media change...
  353.  * PROBLEM: AHDI may get upset if the drive isn't valid.
  354.  * SOLUTION: don't change the default PSEUDODRIVES setting!
  355.  */
  356.     r = mediach(d);
  357.     if (r == 1) {        /* drive _may_ have changed */
  358.         r = rwabs(0, tmpbuf, 1, 0, d, 0L);    /* check the BIOS */
  359.         if (r != E_CHNG) {            /* nope, no change */
  360.             return 0;
  361.         }
  362.         r = 2;            /* drive was definitely changed */
  363.     }
  364.     if (r == 2) {
  365.         fs = drives[d];        /* get filesystem associated with drive */
  366.         if ((*fs->dskchng)(d)) { /* does the fs agree that it changed? */
  367.             drives[d] = 0;
  368.             changedrv(d);    /* yes -- do the change */
  369.             return 1;
  370.         }
  371.     }
  372.     return 0;
  373. }
  374.  
  375. /*
  376.  * routines for parsing path names
  377.  */
  378.  
  379. char temp1[PATH_MAX];    /* temporary storage for file names */
  380.  
  381. #define DIRSEP(p) ((p) == '\\')
  382.  
  383. /*
  384.  * relpath2cookie converts a TOS file name into a file cookie representing
  385.  * the directory the file resides in, and a character string representing
  386.  * the name of the file in that directory. The character string is
  387.  * copied into the "lastname" array. If lastname is NULL, then the cookie
  388.  * returned actually represents the file, instead of just the directory
  389.  * the file is in.
  390.  *
  391.  * note that lastname, if non-null, should be big enough to contain all the
  392.  * characters in "path", since if the file system doesn't want the kernel
  393.  * to do path name parsing we may end up just copying path to lastname
  394.  * and returning the current or root directory, as appropriate
  395.  *
  396.  * "relto" is the directory relative to which the search should start.
  397.  * if you just want the current directory, use path2cookie instead.
  398.  *
  399.  */
  400.  
  401. #define MAX_LINKS 4
  402.  
  403. long
  404. relpath2cookie(relto, path, lastname, res, depth)
  405.     fcookie *relto;
  406.     const char *path;
  407.     char *lastname;
  408.     fcookie *res;
  409.     int depth;
  410. {
  411.     fcookie dir;
  412.     int drv;
  413.     int len;
  414.     char c, *s;
  415.     XATTR xattr;
  416.     static char newpath[16] = "U:\\DEV\\";
  417.     char temp2[PATH_MAX];
  418.     char linkstuff[PATH_MAX];
  419.     long r = 0;
  420.  
  421. /* dolast: 0 == return a cookie for the directory the file is in
  422.  *         1 == return a cookie for the file itself, don't follow links
  423.  *       2 == return a cookie for whatever the file points at
  424.  */
  425.     int dolast = 0;
  426.     int i = 0;
  427.  
  428.     if (!lastname) {
  429.         dolast = 1;
  430.         lastname = temp2;
  431.     } else if (lastname == follow_links) {
  432.         dolast = 2;
  433.         lastname = temp2;
  434.     }
  435.  
  436.     *lastname = 0;
  437.  
  438. PATH2COOKIE_DB(("relpath2cookie(%s, dolast=%d, depth=%d)", path, dolast, depth));
  439.  
  440.     if (depth > MAX_LINKS) {
  441.         DEBUG(("Too many symbolic links"));
  442.         return ELOOP;
  443.     }
  444. /* special cases: CON:, AUX:, etc. should be converted to U:\DEV\CON,
  445.  * U:\DEV\AUX, etc.
  446.  */
  447.     if (strlen(path) == 4 && path[3] == ':') {
  448.         strncpy(newpath+7, path, 3);
  449.         path = newpath;
  450.     }
  451.  
  452. /* first, check for a drive letter */
  453. /* BUG: a '\' at the start of a symbolic link is relative to the current
  454.  * drive of the process, not the drive the link is located on
  455.  */
  456.     if (path[1] == ':') {
  457.         c = path[0];
  458.         if (c >= 'a' && c <= 'z')
  459.             drv = c - 'a';
  460.         else if (c >= 'A' && c <= 'Z')
  461.             drv = c - 'A';
  462.         else
  463.             goto nodrive;
  464.         path += 2;
  465.         i = 1;        /* remember that we saw a drive letter */
  466.     } else {
  467. nodrive:
  468.         drv = curproc->curdrv;
  469.     }
  470.  
  471. /* see if the path is rooted from '\\' */
  472.     if (DIRSEP(*path)) {
  473.         while(DIRSEP(*path))path++;
  474.         dup_cookie(&dir, &curproc->root[drv]);
  475.     } else {
  476.         if (i)    {    /* an explicit drive letter was given */
  477.             dup_cookie(&dir, &curproc->curdir[drv]);
  478.         }
  479.         else
  480.             dup_cookie(&dir, relto);
  481.     }
  482.  
  483.     if (!dir.fs) {
  484.         changedrv(dir.dev);
  485.         dup_cookie(&dir, &curproc->root[drv]);
  486.     }
  487.  
  488.     if (!dir.fs) {
  489.         DEBUG(("path2cookie: no file system: returning EDRIVE"));
  490.         return EDRIVE;
  491.     }
  492.  
  493.     /* here's where we come when we've gone across a mount point */
  494.     
  495. restart_mount:
  496.  
  497.     if (!*path) {        /* nothing more to do */
  498. PATH2COOKIE_DB(("relpath2cookie: no more path, returning 0"));
  499.         *res = dir;
  500.         return 0;
  501.     }
  502.  
  503. /* see if there has been a disk change; if so, return E_CHNG.
  504.  * path2cookie will restart the search automatically; other functions
  505.  * that call relpath2cookie directly will have to fail gracefully
  506.  */
  507.     if (disk_changed(dir.dev)) {
  508.         release_cookie(&dir);
  509. PATH2COOKIE_DB(("relpath2cookie: returning %d", E_CHNG));
  510.         return E_CHNG;
  511.     }
  512.  
  513.  
  514.     if (dir.fs->fsflags & FS_KNOPARSE) {
  515. if (dir.fs != &tos_filesys) DEBUG(("ACK! FS_KNOPARSE but not TOSFS for drive %c:", dir.dev+'A'));
  516.  
  517.         if (!dolast) {
  518. PATH2COOKIE_DB(("fs is a KNOPARSE, nothing to do"));
  519.             strncpy(lastname, path, PATH_MAX-1);
  520.             lastname[PATH_MAX - 1] = 0;
  521.             r = 0;
  522.             *res = dir;
  523.         } else {
  524. PATH2COOKIE_DB(("fs is a KNOPARSE, calling lookup"));
  525.             r = (*dir.fs->lookup)(&dir, path, res);
  526.             release_cookie(&dir);
  527.         }
  528.         if (r == EMOUNT) {    /* hmmm... a ".." at a mount point, maybe */
  529.             fcookie mounteddir;
  530.             r = (*dir.fs->root)(dir.dev, &mounteddir);
  531.             if (r == 0 && drv == UNIDRV) {
  532.                 if (dir.fs == mounteddir.fs &&
  533.                     dir.index == mounteddir.index &&
  534.                     dir.dev == mounteddir.dev) {
  535.                     release_cookie(&dir);
  536.                     release_cookie(&mounteddir);
  537.                     dup_cookie(&dir, &curproc->root[UNIDRV]);
  538.                     TRACE(("path2cookie: restarting from mount point"));
  539.                     goto restart_mount;
  540.                 }
  541.             } else {
  542.                 if (r == 0)
  543.                     release_cookie(&mounteddir);
  544.                 r = 0;
  545.             }
  546.         }
  547.         PATH2COOKIE_DB(("relpath2cookie: returning %ld", r));
  548.         return r;
  549.     }
  550.  
  551.  
  552. /* parse all but (possibly) the last component of the path name */
  553. /* rules here: at the top of the loop, &dir is the cookie of
  554.  * the directory we're in now, xattr is its attributes, and res is unset
  555.  * at the end of the loop, &dir is unset, and either r is nonzero
  556.  * (to indicate an error) or res is set to the final result
  557.  */
  558.     r = (dir.fs->getxattr)(&dir, &xattr);
  559.     if (r) {
  560.         DEBUG(("couldn't get directory attributes"));
  561.         release_cookie(&dir);
  562.         return EINTRN;
  563.     }
  564.  
  565.     while (*path) {
  566.  
  567.     /* now we must have a directory, since there are more things in the path */
  568.         if ((xattr.mode & S_IFMT) != S_IFDIR) {
  569. PATH2COOKIE_DB(("relpath2cookie: not a directory, returning EPTHNF"));
  570.             release_cookie(&dir);
  571.             r = EPTHNF;
  572.             break;
  573.         }
  574.     /* we must also have search permission for the directory */
  575.         if (denyaccess(&xattr, S_IXOTH)) {
  576.             DEBUG(("search permission in directory denied"));
  577.             release_cookie(&dir);
  578.             r = EPTHNF;
  579.             break;
  580.         }
  581.  
  582.     /* if there's nothing left in the path, we can break here */
  583.         if (!*path) {
  584. PATH2COOKIE_DB(("relpath2cookie: no more path, breaking (1)"));
  585.             *res = dir;
  586.             break;
  587.         }
  588.     /* next, peel off the next name in the path */
  589.         len = 0;
  590.         s = lastname;
  591.         c = *path;
  592.         while (c && !DIRSEP(c)) {
  593.             if (len++ < PATH_MAX)
  594.                 *s++ = c;
  595.             c = *++path;
  596.         }
  597.         *s = 0;
  598.  
  599.     /* if there are no more names in the path, and we don't want
  600.      * to actually look up the last name, then we're done
  601.      */
  602.         if (dolast == 0 && !*path) {
  603.             *res = dir;
  604. PATH2COOKIE_DB(("relpath2cookie: no more path, breaking (2)"));
  605.             break;
  606.         }
  607.  
  608.  
  609.     /* 
  610.      * skip trailing slashes
  611.      */
  612.         while (DIRSEP(*path)) path++;
  613.  
  614. PATH2COOKIE_DB(("relpath2cookie: looking up [%s]", lastname));
  615.  
  616.         r = (*dir.fs->lookup)(&dir, lastname, res);
  617.         if (r == EMOUNT) {
  618.             fcookie mounteddir;
  619.             r = (*dir.fs->root)(dir.dev, &mounteddir);
  620.             if (r == 0 && drv == UNIDRV) {
  621.                 if (samefile(&dir, &mounteddir)) {
  622.                     release_cookie(&dir);
  623.                     release_cookie(&mounteddir);
  624.                     dup_cookie(&dir, &curproc->root[UNIDRV]);
  625.                     TRACE(("path2cookie: restarting from mount point"));
  626.                     goto restart_mount;
  627.                 } else if (r == 0) {
  628.                     r = EINTRN;
  629.                     release_cookie(&mounteddir);
  630.                     release_cookie(&dir);
  631.                     break;
  632.                 }
  633.             } else if (r == 0) {
  634.                 release_cookie(&mounteddir);
  635.             } else {
  636.                 release_cookie(&dir);
  637.                 break;
  638.             }
  639.         } else if (r) {
  640.             release_cookie(&dir);
  641.             break;
  642.         }
  643.  
  644.     /* check for a symbolic link */
  645.         r = (res->fs->getxattr)(res, &xattr);
  646.         if (r != 0) {
  647.             DEBUG(("path2cookie: couldn't get file attributes"));
  648.             release_cookie(&dir);
  649.             release_cookie(res);
  650.             break;
  651.         }
  652.  
  653.     /* if the file is a link, and we're following links, follow it */
  654.         if ( (xattr.mode & S_IFMT) == S_IFLNK && (*path || dolast > 1)) {
  655.             r = (res->fs->readlink)(res, linkstuff, PATH_MAX);
  656.             release_cookie(res);
  657.             if (r) {
  658.                 DEBUG(("error reading symbolic link"));
  659.                 release_cookie(&dir);
  660.                 break;
  661.             }
  662.             r = relpath2cookie(&dir, linkstuff, follow_links, res,
  663.                         depth+1);
  664.             release_cookie(&dir);
  665.             if (r) {
  666.                 DEBUG(("error following symbolic link"));
  667.                 break;
  668.             }
  669.             dir = *res;
  670.             (void)(res->fs->getxattr)(res, &xattr);
  671.         } else {
  672.             release_cookie(&dir);
  673.             dir = *res;
  674.         }
  675.     }
  676.  
  677.     PATH2COOKIE_DB(("relpath2cookie: returning %ld", r));
  678.     return r;
  679. }
  680.  
  681. #define MAX_TRYS 8
  682.  
  683. long
  684. path2cookie(path, lastname, res)
  685.     const char *path;
  686.     char *lastname;
  687.     fcookie *res;
  688. {
  689.     fcookie *dir;
  690.     long r;
  691. /* AHDI sometimes will keep insisting that a media change occured;
  692.  * we limit the number of retrys to avoid hanging the system
  693.  */
  694.     int trycnt = 0;
  695.  
  696.     dir = &curproc->curdir[curproc->curdrv];
  697.  
  698.     do {
  699.         r = relpath2cookie(dir, path, lastname, res, 0);
  700.         if (r == E_CHNG)
  701.             DEBUG(("path2cookie: restarting due to media change"));
  702.     } while (r == E_CHNG && trycnt++ < MAX_TRYS);
  703.  
  704.     return r;
  705. }
  706.  
  707. /*
  708.  * release_cookie: tell the file system owner that a cookie is no
  709.  * longer in use by the kernel
  710.  */
  711. void
  712. release_cookie(fc)
  713.     fcookie *fc;
  714. {
  715.     FILESYS *fs;
  716.  
  717.     if (fc) {
  718.         fs = fc->fs;
  719.         if (fs && fs->release) {
  720.             (void)(*fs->release)(fc);
  721.         }
  722.     }
  723. }
  724.  
  725. /*
  726.  * Make a new cookie (newc) which is a duplicate of the old cookie
  727.  * (oldc). This may be something the file system is interested in,
  728.  * so we give it a chance to do the duplication; if it doesn't
  729.  * want to, we just copy.
  730.  */
  731.  
  732. void
  733. dup_cookie(newc, oldc)
  734.     fcookie *newc, *oldc;
  735. {
  736.     FILESYS *fs = oldc->fs;
  737.  
  738.     if (fs && fs->release && fs->dupcookie) {
  739.         (void)(*fs->dupcookie)(newc, oldc);
  740.     } else {
  741.         *newc = *oldc;
  742.     }
  743. }
  744.  
  745. /*
  746.  * new_fileptr, dispose_fileptr: allocate (deallocate) a file pointer
  747.  */
  748.  
  749. FILEPTR *
  750. new_fileptr()
  751. {
  752.     FILEPTR *f;
  753.  
  754.     if ((f = flist) != 0) {
  755.         flist = f->next;
  756.         f->next = 0;
  757.         return f;
  758.     }
  759.     f = kmalloc(SIZEOF(FILEPTR));
  760.     if (!f) {
  761.         FATAL("new_fileptr: out of memory");
  762.     }
  763.     else {
  764.         f->next = 0;
  765.     }
  766.     return f;
  767. }
  768.  
  769. void
  770. dispose_fileptr(f)
  771.     FILEPTR *f;
  772. {
  773.     if (f->links != 0) {
  774.         FATAL("dispose_fileptr: f->links == %d", f->links);
  775.     }
  776.     f->next = flist;
  777.     flist = f;
  778. }
  779.  
  780. /*
  781.  * denyshare(list, f): "list" points at the first FILEPTR in a
  782.  * chained list of open FILEPTRS referring to the same file;
  783.  * f is a newly opened FILEPTR. Every FILEPTR in the given list is
  784.  * checked to see if its "open" mode (in list->flags) is compatible with
  785.  * the open mode in f->flags. If not (for example, if f was opened with
  786.  * a "read" mode and some other file has the O_DENYREAD share mode),
  787.  * then 1 is returned. If all the open FILEPTRs in the list are
  788.  * compatible with f, then 0 is returned.
  789.  * This is not as complicated as it sounds. In practice, just keep a
  790.  * list of open FILEPTRs attached to each file, and put something like
  791.  *     if (denyshare(thisfile->openfileptrlist, newfileptr))
  792.  *        return EACCDN;
  793.  * in the device open routine.
  794.  */
  795.  
  796. int ARGS_ON_STACK 
  797. denyshare(list, f)
  798.     FILEPTR *list, *f;
  799. {
  800.     int newrm, newsm;    /* new read and sharing mode */
  801.     int oldrm, oldsm;    /* read and sharing mode of already opened file */
  802.     int i;
  803.  
  804.     newrm = f->flags & O_RWMODE;
  805.     newsm = f->flags & O_SHMODE;
  806.  
  807. /*
  808.  * O_EXEC gets treated the same as O_RDONLY for our purposes
  809.  */
  810.     if (newrm == O_EXEC) newrm = O_RDONLY;
  811.  
  812. /* New meaning for O_COMPAT: deny write access to all _other_
  813.  * processes.
  814.  */
  815.  
  816.     for ( ; list; list = list->next) {
  817.         oldrm = list->flags & O_RWMODE;
  818.         if (oldrm == O_EXEC) oldrm = O_RDONLY;
  819.         oldsm = list->flags & O_SHMODE;
  820.         if (oldsm == O_DENYW || oldsm == O_DENYRW) {
  821.              if (newrm != O_RDONLY) {
  822.                 DEBUG(("write access denied"));
  823.                 return 1;
  824.             }
  825.         }
  826.         if (oldsm == O_DENYR || oldsm == O_DENYRW) {
  827.             if (newrm != O_WRONLY) {
  828.                 DEBUG(("read access denied"));
  829.                 return 1;
  830.             }
  831.         }
  832.         if (newsm == O_DENYW || newsm == O_DENYRW) {
  833.             if (oldrm != O_RDONLY) {
  834.                 DEBUG(("couldn't deny writes"));
  835.                 return 1;
  836.             }
  837.         }
  838.         if (newsm == O_DENYR || newsm == O_DENYRW) {
  839.             if (oldrm != O_WRONLY) {
  840.                 DEBUG(("couldn't deny reads"));
  841.                 return 1;
  842.             }
  843.         }
  844. /* If either sm == O_COMPAT, then we check to make sure
  845.    that the file pointers are owned by the same process (O_COMPAT means
  846.    "deny writes to any other processes"). This isn't quite the same
  847.    as the Atari spec, which says O_COMPAT means "deny access to other
  848.    processes." We should fix the spec.
  849.  */
  850.         if ((newsm == O_COMPAT && newrm != O_RDONLY && oldrm != O_RDONLY) ||
  851.             (oldsm == O_COMPAT && newrm != O_RDONLY)) {
  852.             for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
  853.                 if (curproc->handle[i] == list)
  854.                     goto found;
  855.             }
  856.         /* old file pointer is not open by this process */
  857.             DEBUG(("O_COMPAT file was opened for writing by another process"));
  858.             return 1;
  859.         found:
  860.             ;    /* everything is OK */
  861.         }
  862.     }
  863.     return 0;
  864. }
  865.  
  866. /*
  867.  * denyaccess(XATTR *xattr, unsigned perm): checks to see if the access
  868.  * specified by perm (which must be some combination of S_IROTH, S_IWOTH,
  869.  * and S_IXOTH) should be granted to the current process
  870.  * on a file with the given extended attributes. Returns 0 if access
  871.  * by the current process is OK, 1 if not.
  872.  */
  873.  
  874. int
  875. denyaccess(xattr, perm)
  876.     XATTR *xattr;
  877.     unsigned perm;
  878. {
  879.     unsigned mode;
  880.  
  881. /* the super-user can do anything! */
  882.     if (curproc->euid == 0)
  883.         return 0;
  884.  
  885.     mode = xattr->mode;
  886.     if (curproc->euid == xattr->uid)
  887.         perm = perm << 6;
  888.     else if (curproc->egid == xattr->gid)
  889.         perm = perm << 3;
  890.     if ((mode & perm) != perm) return 1;    /* access denied */
  891.     return 0;
  892. }
  893.  
  894. /*
  895.  * Checks a lock against a list of locks to see if there is a conflict.
  896.  * This is a utility to be used by file systems, somewhat like denyshare
  897.  * above. Returns 0 if there is no conflict, or a pointer to the
  898.  * conflicting LOCK structure if there is.
  899.  *
  900.  * Conflicts occur for overlapping locks if the process id's are
  901.  * different and if at least one of the locks is a write lock.
  902.  *
  903.  * NOTE: we assume before being called that the locks have been converted
  904.  * so that l_start is absolute. not relative to the current position or
  905.  * end of file.
  906.  */
  907.  
  908. LOCK * ARGS_ON_STACK 
  909. denylock(list, lck)
  910.     LOCK *list, *lck;
  911. {
  912.     LOCK *t;
  913.     unsigned long tstart, tend;
  914.     unsigned long lstart, lend;
  915.     int pid = curproc->pid;
  916.     int ltype;
  917.  
  918.     ltype = lck->l.l_type;
  919.     lstart = lck->l.l_start;
  920.  
  921.     if (lck->l.l_len == 0)
  922.         lend = 0xffffffffL;
  923.     else
  924.         lend = lstart + lck->l.l_len;
  925.  
  926.     for (t = list; t; t = t->next) {
  927.         tstart = t->l.l_start;
  928.         if (t->l.l_len == 0)
  929.             tend = 0xffffffffL;
  930.         else
  931.             tend = tstart + t->l.l_len;
  932.  
  933.     /* look for overlapping locks */
  934.         if (tstart <= lstart && tend >= lstart && t->l.l_pid != pid &&
  935.             (ltype == F_WRLCK || t->l.l_type == F_WRLCK))
  936.             break;
  937.         if (lstart <= tstart && lend >= tstart && t->l.l_pid != pid &&
  938.             (ltype == F_WRLCK || t->l.l_type == F_WRLCK))
  939.             break;
  940.     }
  941.     return t;
  942. }
  943.  
  944. /*
  945.  * check to see that a file is a directory, and that write permission
  946.  * is granted; return an error code, or 0 if everything is ok.
  947.  */
  948. long
  949. dir_access(dir, perm)
  950.     fcookie *dir;
  951.     unsigned perm;
  952. {
  953.     XATTR xattr;
  954.     long r;
  955.  
  956.     r = (*dir->fs->getxattr)(dir, &xattr);
  957.     if (r) {
  958.         DEBUG(("dir_access: file system returned %ld", r));
  959.         return r;
  960.     }
  961.     if ( (xattr.mode & S_IFMT) != S_IFDIR ) {
  962.         DEBUG(("file is not a directory"));
  963.         return EPTHNF;
  964.     }
  965.     if (denyaccess(&xattr, perm)) {
  966.         DEBUG(("no permission for directory"));
  967.         return EACCDN;
  968.     }
  969.     return 0;
  970. }
  971.  
  972. /*
  973.  * returns 1 if the given name contains a wildcard character 
  974.  */
  975.  
  976. int
  977. has_wild(name)
  978.     const char *name;
  979. {
  980.     char c;
  981.  
  982.     while ((c = *name++) != 0) {
  983.         if (c == '*' || c == '?') return 1;
  984.     }
  985.     return 0;
  986. }
  987.  
  988. /*
  989.  * void copy8_3(dest, src): convert a file name (src) into DOS 8.3 format
  990.  * (in dest). Note the following things:
  991.  * if a field has less than the required number of characters, it is
  992.  * padded with blanks
  993.  * a '*' means to pad the rest of the field with '?' characters
  994.  * special things to watch for:
  995.  *    "." and ".." are more or less left alone
  996.  *    "*.*" is recognized as a special pattern, for which dest is set
  997.  *    to just "*"
  998.  * Long names are truncated. Any extensions after the first one are
  999.  * ignored, i.e. foo.bar.c -> foo.bar, foo.c.bar->foo.c.
  1000.  */
  1001.  
  1002. void
  1003. copy8_3(dest, src)
  1004.     char *dest;
  1005.     const char *src;
  1006. {
  1007.     char fill = ' ', c;
  1008.     int i;
  1009.  
  1010.     if (src[0] == '.') {
  1011.         if (src[1] == 0) {
  1012.             strcpy(dest, ".       .   ");
  1013.             return;
  1014.         }
  1015.         if (src[1] == '.' && src[2] == 0) {
  1016.             strcpy(dest, "..      .   ");
  1017.             return;
  1018.         }
  1019.     }
  1020.     if (src[0] == '*' && src[1] == '.' && src[2] == '*' && src[3] == 0) {
  1021.         dest[0] = '*';
  1022.         dest[1] = 0;
  1023.         return;
  1024.     }
  1025.  
  1026.     for (i = 0; i < 8; i++) {
  1027.         c = *src++;
  1028.         if (!c || c == '.') break;
  1029.         if (c == '*') {
  1030.             fill = c = '?';
  1031.         }
  1032.         *dest++ = toupper(c);
  1033.     }
  1034.     while (i++ < 8) {
  1035.         *dest++ = fill;
  1036.     }
  1037.     *dest++ = '.';
  1038.     i = 0;
  1039.     fill = ' ';
  1040.     while (c && c != '.')
  1041.         c = *src++;
  1042.  
  1043.     if (c) {
  1044.         for( ;i < 3; i++) {
  1045.             c = *src++;
  1046.             if (!c || c == '.') break;
  1047.             if (c == '*')
  1048.                 c = fill = '?';
  1049.             *dest++ = toupper(c);
  1050.         }
  1051.     }
  1052.     while (i++ < 3)
  1053.         *dest++ = fill;
  1054.     *dest = 0;
  1055. }
  1056.  
  1057. /*
  1058.  * int pat_match(name, patrn): returns 1 if "name" matches the template in
  1059.  * "patrn", 0 if not. "patrn" is assumed to have been expanded in 8.3
  1060.  * format by copy8_3; "name" need not be. Any '?' characters in patrn
  1061.  * will match any character in name. Note that if "patrn" has a '*' as
  1062.  * the first character, it will always match; this will happen only if
  1063.  * the original pattern (before copy8_3 was applied) was "*.*".
  1064.  *
  1065.  * BUGS: acts a lot like the silly TOS pattern matcher.
  1066.  */
  1067.  
  1068. int
  1069. pat_match(name, template)
  1070.     const char *name, *template;
  1071. {
  1072.     register char *s, c;
  1073.     char expname[TOS_NAMELEN+1];
  1074.  
  1075.     if (*template == '*') return 1;
  1076.     copy8_3(expname, name);
  1077.  
  1078.     s = expname;
  1079.     while ((c = *template++) != 0) {
  1080.         if (c != *s && c != '?')
  1081.             return 0;
  1082.         s++;
  1083.     }
  1084.     return 1;
  1085. }
  1086.  
  1087. /*
  1088.  * int samefile(fcookie *a, fcookie *b): returns 1 if the two cookies
  1089.  * refer to the same file or directory, 0 otherwise
  1090.  */
  1091.  
  1092. int
  1093. samefile(a, b)
  1094.     fcookie *a, *b;
  1095. {
  1096.     if (a->fs == b->fs && a->dev == b->dev && a->index == b->index)
  1097.         return 1;
  1098.     return 0;
  1099. }
  1100.